package com.bflow.web.controller;

import com.bflow.api.IBflowDefineActRouteService;
import com.bflow.api.IBflowDefineProcessService;
import com.bflow.api.bo.BflowDefineActQueryParam;
import com.bflow.api.bo.BflowDefineActRouteBo;
import com.bflow.api.bo.BflowDefineProcessBo;
import com.bflow.api.bo.ext.BflowUserBo;
import com.bflow.api.constant.BflowConstant.ProcessBusiKeyType;
import com.bflow.commons.bean.PageResult;
import com.bflow.util.ActRouteUtil;
import com.bflow.util.AssertUtil;
import com.bflow.util.JsonUtil;
import com.bflow.web.util.TestLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Name 定义流程
 * @Author haowen
 * @Date 2018-12-13
 */
@Controller
@RequestMapping(value = "/bflow/defineProcess")
public class BflowDefineProcessController extends BflowBaseController {

    @Autowired
    private IBflowDefineProcessService<?, BflowDefineProcessBo> bflowDefineProcessService;
    @Autowired
    private IBflowDefineActRouteService<?, BflowDefineActRouteBo> bflowDefineActRouteService;

    @RequestMapping(value = "list")
    @ResponseBody
    public String list(BflowDefineActQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            queryParam.setDefParam(1, 500, "create_time", "DESC");
            queryParam.addExtendParam("appendWhereCondSql", "KEY_ != '"+ ProcessBusiKeyType.LEAVEAPPLY.getCode()+"'");
            PageResult<BflowDefineProcessBo>  pageResult = bflowDefineProcessService.findPage(queryParam);
            return JsonUtil.toStr(pageResult.getResult());
        } catch (Exception e) {
            return returnFail(e, "列表查询失败");
        }
    }

    @RequestMapping(value = "get")
    @ResponseBody
    public String get(String id, Integer version, HttpServletRequest req, HttpServletResponse resp) {
        try {
            Map result = new HashMap<>();
            BflowDefineProcessBo bo = bflowDefineProcessService.detail(id);
            List<BflowDefineActRouteBo> routeBoList = bflowDefineActRouteService.findListByProcId(id, version);
            result.put("bo", bo);
            result.put("routesGroup", ActRouteUtil.buildGroup(routeBoList));
            return returnOk(result, "查询成功");
        } catch (Exception e) {
            return returnFail(e, "查询失败");
        }
    }

    @RequestMapping(value = "validProcKey")
    @ResponseBody
    public String validProcKey(String id, String procKey, HttpServletRequest req, HttpServletResponse resp) {
        boolean valid = false;
        try {
            BflowDefineProcessBo bo = bflowDefineProcessService.getFlowModelBykey(procKey);
            if(bo != null)
                valid = bo.getId().equals(id);
            else
                valid = true;
        } catch (Exception e) {
            logger.error("验证Key失败", e);
        }
        return returnOk(valid, "验证完成");
    }


    @RequestMapping(value = "save")
    @ResponseBody
    public String save(BflowDefineProcessBo bo, HttpServletRequest req, HttpServletResponse resp) {
        try {
            bflowDefineProcessService.save(bo);
            return returnOk("保存成功");
        } catch (Exception e) {
            return returnFail(e, "保存失败");
        }
    }


    @RequestMapping(value = "deploy")
    @ResponseBody
    public String deploy(String id, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(user, "您还未登录, 请先登录");
            bflowDefineProcessService.deploy(id);
            return returnOk("更改成功");
        } catch (Exception e) {
            return returnFail(e, "操作失败");
        }
    }

    @RequestMapping(value = "getSelectList")
    @ResponseBody
    public String getSelectList(HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowDefineActQueryParam queryParam = new BflowDefineActQueryParam();
            queryParam.setDefParam(1, 500, "create_time", "DESC");
            queryParam.addExtendParam("appendWhereCondSql", "KEY_ != '"+ ProcessBusiKeyType.LEAVEAPPLY.getCode()+"'");
            List<BflowDefineProcessBo> list = bflowDefineProcessService.findByParam(queryParam);
            return returnOk(list, "查询成功");
        } catch (Exception e) {
            return returnFail(e, "查询流程下拉框数据失败");
        }
    }

}
